package nix.spring.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import nix.spring.service.UserService;

/**
 * 动态代理方式
 */
public class UserServiceProxyFactory implements InvocationHandler {

	private UserService us;

	public UserServiceProxyFactory(UserService us) {
		super();
		this.us = us;
	}

	public UserService getUserServiceProxy() {

		Object object = Proxy.newProxyInstance(
				this.getClass().getClassLoader(), this.us.getClass()
						.getInterfaces(), this);
		return (UserService) object;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		System.out.println("开启事务！");
		Object object = method.invoke(this.us, args);
		System.out.println("关闭事务！");
		return object;
	}
}
